package com.jyh.service.money;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.db.entity.JyhConfig;
import com.jyh.db.entity.JyhConfigExample;
import com.jyh.db.entity.JyhPlatformAccount;
import com.jyh.db.entity.JyhPlatformAccountExample;
import com.jyh.db.entity.JyhPlatformMoneyLog;
import com.jyh.db.mapper.JyhConfigMapper;
import com.jyh.db.mapper.JyhPlatformAccountMapper;
import com.jyh.db.mapper.JyhPlatformMoneyLogMapper;
import com.jyh.dbOrder.dao.LoanDao;
import com.jyh.util.StringUtils;

@Service
public class AccountService extends BaseAction {
	@Autowired
	private LoanDao loanDao; 
	@Autowired
	private JyhPlatformAccountMapper accountMapper;
	@Autowired
	private JyhConfigMapper configMapper;
	@Autowired
	private JyhPlatformAccountMapper platformAccountMapper;
	@Autowired
	private JyhPlatformMoneyLogMapper platformMoneyLogMapper;
	/**
	 * 获取平台账户管理页面数据
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getAccount(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("money/account");
		List<JyhPlatformAccount> accountList = accountMapper.selectByExample(new JyhPlatformAccountExample());
		mv.addObject("map", accountList.get(0));
		return mv;
	}
	
	/**
	 * 充值页面
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getAccountRecharge(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("money/accountRecharge");
		JyhConfigExample example = new JyhConfigExample();
		example.createCriteria().andGroupsEqualTo("rechargeType");
		List<JyhConfig> rechargeType = configMapper.selectByExample(example);
//		logger.info("获取充值方式rechargeType={}",JSONObject.toJSONString(rechargeType));
		mv.addObject("rechargeTypeList", rechargeType);
		return mv;
	}
	
	/**
	 * 确认充值
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public void getRecharge(HttpServletRequest request, HttpSession session) {
		String account  = request.getParameter("account");
		String rechargeType  = request.getParameter("rechargeType");
		String payMoney  = request.getParameter("payMoney");
		String moneyNO  = request.getParameter("moneyNO");
		String remark  = request.getParameter("remark");
		List<JyhPlatformAccount> list = platformAccountMapper.selectByExample(new JyhPlatformAccountExample());
		JyhPlatformAccount jyhPlatformAccount = new  JyhPlatformAccount();
		// 平台放款账户
		if(StringUtils.isNotNull(account) && account.equals("1")){
			BigDecimal putMoney = list.get(0).getPutMoney().add(new BigDecimal(payMoney));
			jyhPlatformAccount.setPutMoney(putMoney);
		}
		// 平台收入账户
		if(StringUtils.isNotNull(account) && account.equals("2")){
			BigDecimal incomeMoney = list.get(0).getIncomeMoney().add(new BigDecimal(payMoney));
			jyhPlatformAccount.setIncomeMoney(incomeMoney);
		}
		BigDecimal accountBalance = list.get(0).getAccountBalance().add(new BigDecimal(payMoney));
		jyhPlatformAccount.setAccountBalance(accountBalance);
		int count = platformAccountMapper.updateByExampleSelective(jyhPlatformAccount, new JyhPlatformAccountExample());
		if (count <= 0) {
			logger.error("平台账户充值失败");
		}
		JyhPlatformMoneyLog jyhPlatformMoneyLog = new JyhPlatformMoneyLog();
		jyhPlatformMoneyLog.setAccount(Integer.parseInt(account));
		jyhPlatformMoneyLog.setMoneyNO(moneyNO);
		jyhPlatformMoneyLog.setPayMoney(new BigDecimal(payMoney));
		jyhPlatformMoneyLog.setRechargeType(Integer.parseInt(rechargeType));
		jyhPlatformMoneyLog.setRemark(remark);
		jyhPlatformMoneyLog.setTime(new Date());
		jyhPlatformMoneyLog.setOperator(session.getAttribute("userName")+"");
		int id = platformMoneyLogMapper.insertSelective(jyhPlatformMoneyLog);
		if (id <= 0) {
			logger.error("平台资金流水记录表插入充值数据失败");
		}
	}
	
	
	/**
	 * 提现页面
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getAccountWithdraw(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("money/accountWithdraw");
		JyhConfigExample example = new JyhConfigExample();
		example.createCriteria().andGroupsEqualTo("account");
		List<JyhConfig> account = configMapper.selectByExample(example);
//		logger.info("获取账户account={}",JSONObject.toJSONString(account));
		mv.addObject("accountList", account);
		return mv;
	}
	
	/**
	 * 确认提现
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public void getWithdraw(HttpServletRequest request, HttpSession session) {
		String account  = request.getParameter("account");
		String payMoney  = request.getParameter("payMoney");
		String moneyNO  = request.getParameter("moneyNO");
		String remark  = request.getParameter("remark");
		List<JyhPlatformAccount> list = platformAccountMapper.selectByExample(new JyhPlatformAccountExample());
		JyhPlatformAccount jyhPlatformAccount = new  JyhPlatformAccount();
		// 平台放款账户
		if(StringUtils.isNotNull(account) && account.equals("1")){
			BigDecimal putMoney = list.get(0).getPutMoney().subtract(new BigDecimal(payMoney));
			jyhPlatformAccount.setPutMoney(putMoney);
		}
		// 平台收入账户
		if(StringUtils.isNotNull(account) && account.equals("2")){
			BigDecimal incomeMoney = list.get(0).getIncomeMoney().subtract(new BigDecimal(payMoney));
			jyhPlatformAccount.setIncomeMoney(incomeMoney);
		}
		BigDecimal accountBalance = list.get(0).getAccountBalance().subtract(new BigDecimal(payMoney));
		jyhPlatformAccount.setAccountBalance(accountBalance);
		int count = platformAccountMapper.updateByExampleSelective(jyhPlatformAccount, new JyhPlatformAccountExample());
		if (count <= 0) {
			logger.error("平台账户提现失败");
		}
		JyhPlatformMoneyLog jyhPlatformMoneyLog = new JyhPlatformMoneyLog();
		jyhPlatformMoneyLog.setAccount(Integer.parseInt(account));
		jyhPlatformMoneyLog.setMoneyNO(moneyNO);
		jyhPlatformMoneyLog.setPayMoney(new BigDecimal(payMoney));
		jyhPlatformMoneyLog.setRemark(remark);
		jyhPlatformMoneyLog.setTime(new Date());
		jyhPlatformMoneyLog.setOperator(session.getAttribute("userName")+"");
		int id = platformMoneyLogMapper.insertSelective(jyhPlatformMoneyLog);
		if (id <= 0) {
			logger.error("平台资金流水记录表插入提现数据失败");
		}
	}
	
	/**
	 * 转账页面
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getAccountTransfer(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("money/accountTransfer");
		JyhConfigExample example = new JyhConfigExample();
		example.createCriteria().andGroupsEqualTo("account");
		List<JyhConfig> account = configMapper.selectByExample(example);
//		logger.info("获取账户account={}",JSONObject.toJSONString(account));
		mv.addObject("accountList", account);
		return mv;
	}
	
	/**
	 * 确认转账
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public void getTransfer(HttpServletRequest request, HttpSession session) {
		String putAccount  = request.getParameter("account");
		String incomeAccount  = request.getParameter("incomeAccount");
		String payMoney  = request.getParameter("payMoney");
		String moneyNO  = request.getParameter("moneyNO");
		String remark  = request.getParameter("remark");
		List<JyhPlatformAccount> list = platformAccountMapper.selectByExample(new JyhPlatformAccountExample());
		JyhPlatformAccount jyhPlatformAccount = new  JyhPlatformAccount();
		// 平台放款账户
		if(StringUtils.isNotNull(putAccount) && putAccount.equals("1")){
			BigDecimal putMoney = list.get(0).getPutMoney().subtract(new BigDecimal(payMoney));
			BigDecimal incomeMoney = list.get(0).getIncomeMoney().add(new BigDecimal(payMoney));
			jyhPlatformAccount.setPutMoney(putMoney);
			jyhPlatformAccount.setIncomeMoney(incomeMoney);
		}
		// 平台收入账户
		if(StringUtils.isNotNull(putAccount) && putAccount.equals("2")){
			BigDecimal incomeMoney = list.get(0).getIncomeMoney().subtract(new BigDecimal(payMoney));
			BigDecimal putMoney = list.get(0).getPutMoney().add(new BigDecimal(payMoney));
			jyhPlatformAccount.setIncomeMoney(incomeMoney);
			jyhPlatformAccount.setPutMoney(putMoney);
		}
		int count = platformAccountMapper.updateByExampleSelective(jyhPlatformAccount, new JyhPlatformAccountExample());
		if (count <= 0) {
			logger.error("平台账户转账失败");
		}
		JyhPlatformMoneyLog jyhPlatformMoneyLog = new JyhPlatformMoneyLog();
		jyhPlatformMoneyLog.setPutAccount(Integer.parseInt(putAccount));
		jyhPlatformMoneyLog.setIncomeAccount(Integer.parseInt(incomeAccount));
		jyhPlatformMoneyLog.setMoneyNO(moneyNO);
		jyhPlatformMoneyLog.setPayMoney(new BigDecimal(payMoney));
		jyhPlatformMoneyLog.setRemark(remark);
		jyhPlatformMoneyLog.setTime(new Date());
		jyhPlatformMoneyLog.setOperator(session.getAttribute("userName")+"");
		int id = platformMoneyLogMapper.insertSelective(jyhPlatformMoneyLog);
		if (id <= 0) {
			logger.error("平台资金流水记录表插入转账数据失败");
		}
	}
	
	/**
	 * 记录页面
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getAccountRecord(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("money/accountRecord");
		String page = request.getParameter("currentPage");// 传过来的页数
		int lineSize = 20; // 每页显示记录20条数
		int pageCount = 0; // 总页数
		int count = 0; // 数据总数
		int currentPage = 1; // 当前是第几页,默认是第一页
		if (page == null || "1".equals(page)) {
		} else {
			currentPage = Integer.parseInt(page); // 当前是第几页,默认是第一页
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		String limitSql = " limit " + (currentPage - 1) * lineSize + "," + lineSize;
		String sql1 = "select count(1) as count from jyh_platform_money_log t1 left join "
				+ "(select * from jyh_config where groups='type') t2 on t1.type=t2.value";
		
		String sql = "select * from jyh_platform_money_log t1 left join "
				+ "(select * from jyh_config where groups='type') t2 on t1.type=t2.value";
		sql = sql + limitSql;
		List<Map<String, Object>> list = loanDao.getList(sql);
		Map<String, Object> map = loanDao.getMap(sql1);
		count = Integer.parseInt(map.get("count") + "");
		int temp = count / lineSize; // 取商
		int tem2 = count % lineSize;// 取余数
		if (tem2 > 0 && tem2 < lineSize && temp > 0) {
			pageCount = count / lineSize + 1; // 总页数
		} else if (tem2 == 0 && temp > 0) {
			pageCount = temp;
		} else {
			pageCount = 1; // 总页数
		}
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		}
		mv.addObject("pageCount", pageCount);
		mv.addObject("currentPage", currentPage);
		mv.addObject("count", count);
		mv.addObject("list", list);
		return mv;
	}
}
